package com.zxl.live.screen.ui.helper;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;

import com.zxl.live.screen.ui.activity.FeedbackActivity;
import com.zxl.live.screen.ui.dialog.GuideDialog;
import com.zxl.live.screen.ui.dialog.GuideDialogListener;
import com.zxl.live.screen.ui.dialog.GuideFeedBackDialog;
import com.zxl.live.screen.ui.dialog.GuideMarkAgainDialog;
import com.zxl.live.screen.ui.dialog.GuideMarkDialog;
import com.zxl.live.tools.track.StatisticsConstant;
import com.zxl.live.tools.track.StatisticsType;

/**
 * 引导弹窗控制
 * 主动引导：首次双开完成回到应用首页
 * 被动引导：用户点击评分按钮或者banner触发
 * @author linzewu
 * @date 16-8-12
 */
public class GuideController implements GuideInterface {
    
    private static final int MODE_AUTO_GUIDE_SHOW_ALL_DIALOG = 0;   //主动引导逻辑，显示除了弹框4之外的弹框
    private static final int MODE_AUTO_GUIDE_SHOW_NO_DIALOG = 1;    //主动引导逻辑，不显示任何弹框
    private static final int MODE_AUTO_GUIDE_SHOW_FINAL_DIALOG = 2; //主动引导逻辑，显示弹框4
    private static final long AUTO_DIALOG_SHOW_DELAY_TIME = 2000;   //主动引导延迟时间

    private boolean mIsAutoShow = false;     //是否主动引导
    private boolean mIsCancel = false;       //是否取消引导

    private Handler mMainHandler;
    private Context mContext;
    private GuideDialog mGuideDialog;
    private GuideFeedBackDialog mGuideFeedBackDialog;
    private GuideMarkDialog mGuideMarkDialog;
    private GuideMarkAgainDialog mGuideMarkAgainDialog;
    
    public GuideController(Context context) {
        this.mContext = context;
        this.mMainHandler = new Handler();
    }

    public boolean isShowGuide() {
        return MarketUrlHelper.getInstance().haveMarket();
    }
    
    /**
     * 公开方法：引导弹窗完整流程
     * @param isAutoShow 是否主动弹窗，点击评分按钮显示则传false
     */
    @Override
    public void showGuide(boolean isAutoShow) {
        if (!isShowGuide()) {
            return;
        }
        this.mIsAutoShow = isAutoShow;
        this.mIsCancel = false;
        if (isAutoShow) {
            /* 主动显示弹窗引导评分 */
            autoShowGuide();
        } else {
            /* 被动显示弹窗引导评分 */
            passiveShowGuide();
        }
    }

    /**
     * 公开方法：取消引导弹框
     */
    @Override
    public void cancelGuide() {
        this.mIsCancel = true;
    }

    /**
     * 公开方法：调起下次启动的主动引导
     */
    @Override
    public void callGuide() {
        if (!SpManager.getInstance(mContext).getHasDoubleAppUse()) {
            SpManager.getInstance(mContext).setIsAutoGuideNeedShow(true);
            SpManager.getInstance(mContext).setHasDoubleAppUse(true);
        }
    }
    
    private void autoShowGuide() {
        mMainHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (mIsCancel) {
                    return ;
                }
                /* 如果用户已经用过一次双开，主动弹窗 */
                if (SpManager.getInstance(mContext).getIsAutoGuideNeedShow()) {
                    SpManager.getInstance(mContext).setIsAutoGuideNeedShow(false);
                    int mode = SpManager.getInstance(mContext).getAutoGuideMode();
                    if (mode == MODE_AUTO_GUIDE_SHOW_ALL_DIALOG) {
                        showGuideDialog();
                    } else if (mode == MODE_AUTO_GUIDE_SHOW_NO_DIALOG) {
                        //do nothing
                    } else if (mode == MODE_AUTO_GUIDE_SHOW_FINAL_DIALOG) {
                        showMarkAgainGuide();
                    }
                }
            }
        }, AUTO_DIALOG_SHOW_DELAY_TIME);
    }
    
    private void passiveShowGuide() {
        mMainHandler.post(new Runnable() {
            @Override
            public void run() {
                if (mIsCancel) {
                    return ;
                }
                showGuideDialog();
            }
        });
    }
    
    private void showGuideDialog() {
        mGuideDialog = new GuideDialog(mContext);
        mGuideDialog.setGuideDialogListener(new GuideDialogListener() {
            @Override
            public void onConfirm() {
                mGuideDialog.dismiss();
                showGuideMarkDialog();
            }

            @Override
            public void onCancel() {
                mGuideDialog.dismiss();
                showGuideFeedBackDialog();
                if (!mIsAutoShow) {
                    SpManager.getInstance(mContext).setAutoGuideMode(MODE_AUTO_GUIDE_SHOW_NO_DIALOG);
                }
            }

            @Override
            public void onTouchExternal() {
                if (!mIsAutoShow) {
                    SpManager.getInstance(mContext).setAutoGuideMode(MODE_AUTO_GUIDE_SHOW_NO_DIALOG);
                }
            }
        });
        mGuideDialog.show();
    }

    private void showMarkAgainGuide() {
        mGuideMarkAgainDialog = new GuideMarkAgainDialog(mContext);
        mGuideMarkAgainDialog.setGuideDialogListener(new GuideDialogListener() {
            @Override
            public void onConfirm() {
                mGuideMarkAgainDialog.dismiss();
                StatisticsType.HOME_PAGE.upload(mContext,  StatisticsConstant.Event.SCORE, StatisticsConstant.Event.SCORE_GO_MARKET);
                MarketUrlHelper.getInstance().gotoMarket(mContext, mContext.getPackageName());
            }

            @Override
            public void onCancel() {
                mGuideMarkAgainDialog.dismiss();
                StatisticsType.HOME_PAGE.upload(mContext,  StatisticsConstant.Event.SCORE, StatisticsConstant.Event.SCORE_CANCEL_MARKET);
            }

            @Override
            public void onTouchExternal() {
                
            }
        });
        mGuideMarkAgainDialog.show();
    }

    private void showGuideFeedBackDialog() {
        mGuideFeedBackDialog = new GuideFeedBackDialog(mContext);
        mGuideFeedBackDialog.setGuideDialogListener(new GuideDialogListener() {
            @Override
            public void onConfirm() {
                mGuideFeedBackDialog.dismiss();
                mContext.startActivity(new Intent(mContext, FeedbackActivity.class));
                StatisticsType.HOME_PAGE.upload(mContext,  StatisticsConstant.Event.SCORE, StatisticsConstant.Event.SCORE_GO_FEEDBACK);
            }

            @Override
            public void onCancel() {
                mGuideFeedBackDialog.dismiss();
                StatisticsType.HOME_PAGE.upload(mContext,  StatisticsConstant.Event.SCORE, StatisticsConstant.Event.SCORE_CANCEL_FEEDBACK);
            }

            @Override
            public void onTouchExternal() {
                
            }
        });
        mGuideFeedBackDialog.show();
    }
    
    private void showGuideMarkDialog() {
        mGuideMarkDialog = new GuideMarkDialog(mContext);
        mGuideMarkDialog.setGuideDialogListener(new GuideDialogListener() {
            @Override
            public void onConfirm() {
                mGuideMarkDialog.dismiss();
                MarketUrlHelper.getInstance().gotoMarket(mContext, mContext.getPackageName());
                StatisticsType.HOME_PAGE.upload(mContext,  StatisticsConstant.Event.SCORE, StatisticsConstant.Event.SCORE_GO_MARKET);
                if (!mIsAutoShow) {
                    SpManager.getInstance(mContext).setAutoGuideMode(MODE_AUTO_GUIDE_SHOW_NO_DIALOG);
                }
            }

            @Override
            public void onCancel() {
                mGuideMarkDialog.dismiss();
                StatisticsType.HOME_PAGE.upload(mContext,  StatisticsConstant.Event.SCORE, StatisticsConstant.Event.SCORE_CANCEL_MARKET);
                if (!mIsAutoShow) {
                    SpManager.getInstance(mContext).setAutoGuideMode(MODE_AUTO_GUIDE_SHOW_FINAL_DIALOG);
                }
            }

            @Override
            public void onTouchExternal() {
                if (!mIsAutoShow) {
                    SpManager.getInstance(mContext).setAutoGuideMode(MODE_AUTO_GUIDE_SHOW_FINAL_DIALOG);
                }
            }
        });
        mGuideMarkDialog.show();
    }
}
